import request from '@/utils/request'
import {ElMessage} from "element-plus";
import _ from 'lodash'
import {calcGreek, expandGreek} from '@/api/calc/otcOption/calcGreek'
import { isNotVoid } from '../../../utils/util';

// 获取定价参数
export function getDefaultPricingParam(otcOptionJson) {
    return request({
        url: 'otcOption/calcMarket/getCalcPricingParamByCfgId.action',
        method: 'post',
        data: otcOptionJson
    })
}
/*获取 曲线或曲面插值*/
export function calcVolSurfaceRateBase64(data) {
    return request({
        url: '/otcOption/calcMarket/calcVolSurfaceRateBase64.action',
        method: 'post',
        data: data
    })
}/*获取分红曲线插值*/
export function calcDivCurveRateBase64(data) {
    return request({
        url: '/otcOption/calcMarket/calcDivCurveRateBase64.action',
        method: 'post',
        data: data
    })
}/*获取曲线插值*/
export function calcCurveRateBase64(data) {
    return request({
        url: '/otcOption/calcMarket/calcCurveRateBase64.action',
        method: 'post',
        data: data
    })
}

/**
 * 获取交易日历  目前后台写死CHINA_EX
 * data:  {
    startDay:'YYYY-MM-DD',
    endDay:'YYYY-MM-DD'
}
 */
export function getCalendar(data) {
    return request({
        url: 'otcOption/calcMarket/getCalendar.action',
        method: 'post',
        data: data
    })
}

// 获取一个合理的交易日  目前后台写死CHINA_EX
// data:{calDay:"2022-xx-xx"}
export function getTerminationPaymentDate(date) {
    return request({
        url: 'otcOption/calcMarket/getTerminationPaymentDate.action',
        method: 'post',
        data: date
    })
}

// 获取一个合理的交易日  目前后台写死CHINA_EX
// data:{calDay:"2022-xx-xx"}
export function getOffset(date) {
    return request({
        url: 'otcOption/calcMarket/getOffset.action',
        method: 'post',
        data: date
    })
}


// 试算
export function calcMarket(otcOptionJson) {
    return new Promise((resolve, reject)=>{
        request({
            url: 'otcOption/calcMarket.action',
            method: 'post',
            data: otcOptionJson
        }).then(response=>{
            if (response.code != '-1') {
                let res = response;
                let result = res.result;
                result.contract.optionEntitlement = isNotVoid(result.contract.optionEntitlement) ? result.contract.optionEntitlement : otcOptionJson.calcInstrumentForm.optionEntitlement;
                // 不同的期权对于希腊字母的计算方式不一样
                result = calcGreek(otcOptionJson, result)
                //对于计算服务返回的值进行加工,在原有的希腊字母进行扩展
                result = expandGreek(otcOptionJson, result)
            }
            resolve(response)
        })
    })
}


// 反算合约要素
export function calcContractParam(otcOptionJson) {
    return request({
        url: 'otcOption/calcContractParam.action',
        method: 'post',
        data: otcOptionJson
    })
}

export async function calcContractParamProxy(otcOptionJson) {
    return new Promise((resolve) => {
        calcContractParam(otcOptionJson).then((response) => {
            if (response.code != 0) {
                ElMessage.warning("当前NPV:"+otcOptionJson.calcPricingParam.marketNPV+"; 无对应反算解，请调整NPV至合理范围。");
            } else {
                let result = response.result;
                if (Boolean(result)) {
                    resolve(result)
                }
            }
        })
    })
}

// 试算
export function marketNPV(otcOptionJson) {
    return request({
        url: 'otcOption/calcMarketVol.action',
        method: 'post',
        data: otcOptionJson
    })
}

// 情景试算
export function calcScene(otcOptionJson) {
    return request({
        url: 'otcOption/calcScene.action',
        method: 'post',
        data: otcOptionJson
    })
}

// 情景试算
export function calcPaymentInfo(otcOptionJson) {
    return request({
        url: 'otcOption/calcPaymentInfo.action',
        method: 'post',
        data: otcOptionJson
    })
}


// 情景试算
export function calcScene2GetResult(otcOptionJson) {
    return request({
        url: 'otcOption/calcScene2GetResult.action',
        method: 'post',
        data: otcOptionJson
    })
}
// 试算
export function saveCalcLogs(otcOptionJson) {
    return request({
        url: 'calcLogs/saveCalcLogs.action',
        method: 'post',
        data: otcOptionJson
    })
}


export function getInstrumentList(param) {
    return request({
        url: 'instrument/getInstrumentList.action',
        method: 'get',
        params: param
    })
}

export function getDailyPrice(param) {
    return request({
        url: 'dailyPrice/getDailyPrice.action',
        method: 'get',
        params: param
    })
}
export function getOptionList(param) {
    return request({
        url: 'instrument/getOptionList.action',
        method: 'get',
        params: param
    })
}
export function getTradeOptionList(param) {
    return request({
        url: 'instrument/getTradeOptionList.action',
        method: 'get',
        params: param
    })
}

export function getIrCurveList(param) {
    return request({
        url: '/ircurve/queryTirCurveInfoByPage.action',
        method: 'get',
        params: param
    })
}

export function getCalcLogs(param) {
    return request({
        url: '/calcLogs/queryCalcLogs.action',
        method: 'get',
        params: param
    })
}

export function getOtcOptionCalcLogsCount(param) {
    return request({
        url: '/calcLogs/getOtcOptionCalcLogsCount.action',
        method: 'get',
        params: param
    })
}

export function deleteCalcLogs(param) {
    return request({
        url: '/calcLogs/deleteCalcLogs.action',
        method: 'get',
        params: param
    })
}

// "<?xml version=\"1.0\" encoding=\"GB2312\"?><root><Parameter><KeyValues><MCPathNum DATATYPE = \"INT\" TYPE = \"SIMPLE\" >100000</MCPathNum><VarianceReductionMethod DATATYPE = \"INT\" TYPE = \"SIMPLE\" >0</VarianceReductionMethod></KeyValues></Parameter></root>"
// "<?xml version=\"1.0\" encoding=\"GB2312\"?><root><Parameter><KeyValues><GridSize DATATYPE=\"INT\" TYPE=\"SIMPLE\">256</GridSize><RangeScale DATATYPE=\"INT\" TYPE=\"SIMPLE\">10</RangeScale></KeyValues></Parameter></root>"
